﻿using AutoMapper;
using BanquetPortion.Dtos.FromBanquetGiftMoneyBookDto;
using BanquetPortion.Dtos.FromDataDictionaryDto;
using BanquetPortion.Models;
using BanquetPortion.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BanquetPortion.Controllers
{
    [Route("api/banquetGiftMoneyBooks")]
    [ApiController]
    public class BanquetGiftMoneyBooksController : ControllerBase
    {
        private readonly IBanquetGiftMoneyBookRepository _banquetGiftMoneyBookRepository;
        private readonly IMapper _mapper;
        public BanquetGiftMoneyBooksController(
            IBanquetGiftMoneyBookRepository banquetGiftMoneyBookRepository,
            IMapper mapper)
        {
            _banquetGiftMoneyBookRepository = banquetGiftMoneyBookRepository;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> GetBanquetGiftMoneyBooksAsync()
        {
            var banquetGiftMoneyBooksFromRepo = await _banquetGiftMoneyBookRepository.GetBanquetGiftMoneyBooksAsync();
            if (banquetGiftMoneyBooksFromRepo == null)
            {
                return NotFound("还未存入数据");
            }
            var banquetGiftMoneyBooksDto = _mapper.Map<IEnumerable<BanquetGiftMoneyBookDto>>(banquetGiftMoneyBooksFromRepo);
            return Ok(banquetGiftMoneyBooksDto);
        }
        [HttpGet("{banquetGiftMoneyBookId}", Name =nameof(GetBanquetGiftMoneyBookAsync))]
        public async Task<IActionResult> GetBanquetGiftMoneyBookAsync(int banquetGiftMoneyBookId)
        {
            var banquetGiftMoneyBookFromRepo = await _banquetGiftMoneyBookRepository.GetBanquetGiftMoneyBookByIdAsync(banquetGiftMoneyBookId);
            if (banquetGiftMoneyBookFromRepo == null)
            {
                return NotFound($"未找到 {banquetGiftMoneyBookId} 资源");
            }
            var banquetGiftMoneyBookDto = _mapper.Map<BanquetGiftMoneyBookDto>(banquetGiftMoneyBookFromRepo);
            return Ok(banquetGiftMoneyBookDto);
        }
        [HttpPut("{banquetGiftMoneyBookId}")]
        public async Task<IActionResult> UpdateBanquetGiftMoneyBookAsync([FromRoute] int banquetGiftMoneyBookId, [FromBody] BanquetGiftMoneyBookForUpdateDto banquetGiftMoneyBookForUpdateDto)
        {
            if (!await _banquetGiftMoneyBookRepository.BanquetGiftMoneyBookExistsAsync(banquetGiftMoneyBookId))
            {
                return NotFound("不存在该资源。");
            }
            var banquetGiftMoneyBookFromRepo = await _banquetGiftMoneyBookRepository.GetBanquetGiftMoneyBookByIdAsync(banquetGiftMoneyBookId);
            _mapper.Map(banquetGiftMoneyBookForUpdateDto, banquetGiftMoneyBookFromRepo);
            await _banquetGiftMoneyBookRepository.SaveAsync();
            var banquetGiftMoneyBook = await _banquetGiftMoneyBookRepository.GetBanquetGiftMoneyBookByIdAsync(banquetGiftMoneyBookId);
            var banquetGiftMoneyBookDto = _mapper.Map<BanquetGiftMoneyBookDto>(banquetGiftMoneyBook);
            return Ok(banquetGiftMoneyBookDto);
        }
        [HttpPost]
        public async Task<IActionResult> CreateBanquetGiftMoneyBookAsync([FromBody] BanquetGiftMoneyBookForCreateDto banquetGiftMoneyBookForCreateDto)
        {
            var banquetGiftMoneyBookModel = _mapper.Map<BanquetGiftMoneyBook>(banquetGiftMoneyBookForCreateDto);
            _banquetGiftMoneyBookRepository.CreateBanquetGiftMoneyBook(banquetGiftMoneyBookModel);
            await _banquetGiftMoneyBookRepository.SaveAsync();
            var banquetGiftMoneyBookToRoute = _mapper.Map<BanquetGiftMoneyBookForCreateDto>(banquetGiftMoneyBookModel);
            return CreatedAtRoute(nameof(GetBanquetGiftMoneyBookAsync),
                new { banquetGiftMoneyBookId = banquetGiftMoneyBookToRoute.BanquetType }, banquetGiftMoneyBookToRoute);
        }
        [HttpDelete("{banquetGiftMoneyBookId}")]
        public async Task<IActionResult> DeleteBanquetGiftMoneyBookAsync([FromRoute] int banquetGiftMoneyBookId)
        {
            //if (!_banquetGiftMoneyBookRepository.BanquetGiftMoneyBookExists(banquetGiftMoneyBookId))
            //{
            //    return NotFound("不存在该资源!");
            //}
            var banquetGiftMoneyBookFromRepo = await _banquetGiftMoneyBookRepository.GetBanquetGiftMoneyBookByIdAsync(banquetGiftMoneyBookId);
            _banquetGiftMoneyBookRepository.DeleteBanquetGiftMoneyBook(banquetGiftMoneyBookFromRepo);
            await _banquetGiftMoneyBookRepository.SaveAsync();
            return NoContent();
        }
    }
}
